package com.ly.web.controller.erp;

import java.util.List;
import java.util.concurrent.Semaphore;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ly.common.core.page.TableDataInfo;
import com.ly.heart.domain.dto.KingInventoryQueryDto;
import com.ly.heart.domain.dto.KingOutInventoryQueryDto;
import com.ly.heart.domain.vo.MenuKingInventoryMaterialVo;
import com.ly.heart.domain.vo.select.query.KingInventorySelectQueryDto;
import com.ly.heart.service.IKingInventoryService;
import com.ly.heart.service.king.KingInventoryPushService;
import com.ly.heart.service.king.SaleInventoryPushService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ly.common.annotation.Log;
import com.ly.common.core.controller.BaseController;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.enums.BusinessType;
import com.ly.heart.domain.KingInventoryEntity;
import com.ly.common.utils.poi.ExcelUtil;

import javax.annotation.Resource;

/**
 * 金蝶-入库/出库单Controller
 *
 * @author cxl
 * @date 2024-07-13
 */
@Api ( value = "金蝶入库/出库单", tags = "金蝶入库/出库单" )
@RestController
@RequestMapping ( "/heart/KingInventory" )
public class KingInventoryController extends BaseController {

    @Resource
    private IKingInventoryService kingInventoryService;
    @Resource
    private KingInventoryPushService kingInventoryPushService;
    @Resource
    private SaleInventoryPushService saleInventoryPushService;


//    @PreAuthorize("@ss.hasPermi('heart:KingInventory:list')")
//    @PostMapping ("/list")
//    @ApiOperation ("查询金蝶入库/出库单列表")
//    public AjaxResult getKingInventoryPageList(@RequestBody KingInventoryQueryDto queryDto){
//        return kingInventoryService.getKingInventoryPageList(queryDto);
//    }


    @PreAuthorize ( "@ss.hasPermi('heart:KingInventory:list')" )
    @PostMapping ( "/list" )
    @ApiOperation ( "查询采购入库单列表" )
    public AjaxResult getKingInventoryPageListByMaterial ( @RequestBody KingInventoryQueryDto queryDto ) {
        return kingInventoryService.getKingInventoryPageListByMaterial ( queryDto );
    }

    @PostMapping ( "/select" )
    @ApiOperation ( "采购入库单选单接口" )
    public AjaxResult getKingInventorySelect ( @RequestBody KingInventorySelectQueryDto queryDto ) {
        return kingInventoryService.getKingInventorySelect ( queryDto );
    }

    @PreAuthorize ( "@ss.hasPermi('heart:KingInventory:add')" )
    @Log ( title = "暂存采购入库单", businessType = BusinessType.INSERT )
    @PostMapping ( "/draftKingInventory" )
    @ApiOperation ( "暂存采购入库单" )
    public AjaxResult draftKingInventory ( @RequestBody KingInventoryEntity kingInventory ) {
        return kingInventoryService.draftKingInventory ( kingInventory );
    }

    @PreAuthorize ( "@ss.hasPermi('heart:KingInventory:add')" )
    @Log ( title = "提交采购入库单", businessType = BusinessType.INSERT )
    @PostMapping ( "/submitKingInventory" )
    @ApiOperation ( "提交采购入库单" )
    public AjaxResult submitKingInventory ( @RequestBody KingInventoryEntity kingInventory ) {
        return kingInventoryService.submitKingInventory ( kingInventory );
    }

    @PreAuthorize ( "@ss.hasPermi('heart:KingInventory:edit')" )
    @Log ( title = "修改采购入库单", businessType = BusinessType.UPDATE )
    @PostMapping ( "/editKingInventory" )
    @ApiOperation ( "修改采购入库单" )
    public AjaxResult updateKingInventory ( @RequestBody KingInventoryEntity kingInventory ) {
        return kingInventoryService.updateKingInventory ( kingInventory );
    }

    @PreAuthorize ( "@ss.hasPermi('heart:KingInventory:list')" )
    @PostMapping ( "/getKingSaleOutInventoryPageList" )
    @ApiOperation ( "查询销售出库单列表" )
    public AjaxResult getKingSaleOutInventoryPageList ( @RequestBody KingInventoryQueryDto queryDto ) {
        return kingInventoryService.getKingSaleOutInventoryPageList ( queryDto );
    }

    @PreAuthorize ( "@ss.hasPermi('heart:KingInventory:add')" )
    @Log ( title = "暂存销售出库单", businessType = BusinessType.INSERT )
    @PostMapping ( "/saleDraftKingInventory" )
    @ApiOperation ( "暂存销售出库单" )
    public AjaxResult saleDraftKingInventory ( @RequestBody KingInventoryEntity kingInventory ) {
        return kingInventoryService.saleDraftKingInventory ( kingInventory );
    }

    @PreAuthorize ( "@ss.hasPermi('heart:KingInventory:add')" )
    @Log ( title = "提交销售出库单", businessType = BusinessType.INSERT )
    @PostMapping ( "/saleSubmitKingInventory" )
    @ApiOperation ( "提交销售出库单" )
    public AjaxResult saleSubmitKingInventory ( @RequestBody KingInventoryEntity kingInventory ) {
        return kingInventoryService.saleSubmitKingInventory ( kingInventory );
    }


//    @PreAuthorize("@ss.hasPermi('heart:KingInventory:saveSaleKingInventory')")
//    @Log(title = "金蝶销售出库单", businessType = BusinessType.UPDATE)
//    @PostMapping("/saveSaleKingInventory")
//    @ApiOperation("新增金蝶销售出库单")
//    public AjaxResult saveSaleKingInventory(@RequestBody KingInventoryEntity kingInventory){
//        return kingInventoryService.saveSaleKingInventory(kingInventory);
//    }


    /**
     * 获取金蝶-入库/出库单详细信息
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingInventory:query')" )
    @GetMapping ( value = "/{id}" )
    @ApiOperation ( "获取金蝶-入库/出库单详细信息" )
    public AjaxResult getInfo ( @PathVariable ( "id" ) String id ) {
        return AjaxResult.success ( kingInventoryService.selectKingInventoryEntityById ( id ) );
    }

//    /**
//     * 查询金蝶-入库/出库单列表
//     */
//    @PreAuthorize("@ss.hasPermi('heart:KingInventory:list')")
//    @GetMapping("/list")
//    @ApiOperation("查询金蝶-入库/出库单列表")
//    public TableDataInfo list(KingInventoryEntity kingInventoryEntity)
//    {
//        startPage();
//        List<KingInventoryEntity> list = kingInventoryService.selectKingInventoryEntityList(kingInventoryEntity);
//        return getDataTable(list);
//    }

    /**
     * 导出金蝶-入库/出库单列表
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingInventory:export')" )
    @Log ( title = "金蝶-入库/出库单", businessType = BusinessType.EXPORT )
    @GetMapping ( "/export" )
    @ApiOperation ( "导出金蝶-入库/出库单列表" )
    public AjaxResult export ( KingInventoryEntity kingInventoryEntity ) {
        List<KingInventoryEntity> list = kingInventoryService.selectKingInventoryEntityList ( kingInventoryEntity );
        ExcelUtil<KingInventoryEntity> util = new ExcelUtil<KingInventoryEntity> ( KingInventoryEntity.class );
        return util.exportExcel ( list, "KingInventory" );
    }


//
//    /**
//     * 新增金蝶-入库/出库单
//     */
//    @PreAuthorize("@ss.hasPermi('heart:KingInventory:add')")
//    @Log(title = "金蝶-入库/出库单", businessType = BusinessType.INSERT)
//    @PostMapping
//    @ApiOperation("新增金蝶-入库/出库单")
//    public AjaxResult add(@RequestBody KingInventoryEntity kingInventoryEntity)
//    {
//        return toAjax(kingInventoryService.insertKingInventoryEntity(kingInventoryEntity));
//    }

    /**
     * 修改金蝶-入库/出库单
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingInventory:edit')" )
    @Log ( title = "金蝶-入库/出库单", businessType = BusinessType.UPDATE )
    @PutMapping
    @ApiOperation ( "修改金蝶-入库/出库单" )
    public AjaxResult edit ( @RequestBody KingInventoryEntity kingInventoryEntity ) {
        return toAjax ( kingInventoryService.updateKingInventoryEntity ( kingInventoryEntity ) );
    }

    /**
     * 删除金蝶-入库/出库单
     */
    @PreAuthorize ( "@ss.hasPermi('heart:KingInventory:remove')" )
    @Log ( title = "金蝶-入库/出库单", businessType = BusinessType.DELETE )
    @DeleteMapping ( "/{ids}" )
    @ApiOperation ( "删除金蝶-入库/出库单" )
    public AjaxResult remove ( @PathVariable String[] ids ) {
        return toAjax ( kingInventoryService.deleteKingInventoryEntityByIds ( ids ) );
    }


    @PreAuthorize ( "@ss.hasPermi('heart:KingInventory:list')" )
    @PostMapping ( "/getMenuSaleInventoryList" )
    @ApiOperation ( "选单调用销售出库单列表" )
    public AjaxResult getMenuSaleInventoryList ( @RequestBody KingInventoryEntity kingInventoryEntity ) {
        return kingInventoryService.getMenuSaleInventoryList ( kingInventoryEntity );
    }

    @GetMapping ( "getInventoryById" )
    @ApiOperation ( "查询销售出库单详情" )
    public AjaxResult getInventoryById ( String id ) {
        return AjaxResult.success ( kingInventoryService.getInventoryById ( id ) );
    }

    @GetMapping ( "send" )
    @ApiOperation ( "销售出库单推送金蝶" )
    public AjaxResult send ( String id ) {
        saleInventoryPushService.push ( id );
        return AjaxResult.success ( "推送成功" );
    }

    @GetMapping ("/push/{kingInventoryId}")
    @ApiOperation ("采购入库单推送")
    public AjaxResult push(@PathVariable String kingInventoryId){
        kingInventoryPushService.push(kingInventoryId);
        return AjaxResult.success("推送成功！");
    }


}
